package com.vmove.childtrain.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vmove.childtrain.R;
import com.vmove.childtrain.common.BaseFragment;
import com.vmove.childtrain.common.BaseSerialActivity;
import com.vmove.childtrain.common.Constants;
import com.vmove.childtrain.db.dao.LightDao;
import com.vmove.childtrain.db.dao.SettingDao;
import com.vmove.childtrain.db.entity.Light;
import com.vmove.childtrain.db.entity.Setting;
import com.vmove.childtrain.protocol.PadIDCMD;

import java.sql.SQLException;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置页面
 */
public class ThirdFragment extends BaseFragment {


    @BindView(R.id.tv_padId)
    TextView tvPadId;
    @BindView(R.id.iv_editPadId)
    ImageView ivEditPadId;
    @BindView(R.id.tv_lightNum)
    TextView tvLightNum;
    @BindView(R.id.iv_editLightNum)
    ImageView ivEditLightNum;
    @BindView(R.id.tv_aboutus)
    TextView tvAboutus;

    private LightDao lightDao;
    private SettingDao settingDao;

    private int oldLightNum;
    private String oldPadId="";
    private String oldInterval="";
    private List<Light> allLights = null;

    @Override
    protected View loadView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_third, null);
    }


    @Override
    protected void bindView(View rootView) {
        initData();
    }

    private void initData() {
        lightDao = new LightDao(mContext);
        settingDao = new SettingDao(mContext);

        Setting settingPadId = settingDao.getSettingByName(Setting.NAME_PADID);
        Setting settingInterval = settingDao.getSettingByName(Setting.NAME_INTERVAL);
        if (null!=settingPadId){
            oldPadId = settingPadId.getSettingValue();
        }else {
            oldPadId = Constants.PADID+"";
        }
        if (null!=settingInterval) oldInterval = settingInterval.getSettingValue();
        tvPadId.setText(oldPadId+"");
//        mEtInterval.setText(oldInterval+"");
        try {
            allLights = lightDao.getAllLights();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        oldLightNum = allLights.size();
        tvLightNum.setText(oldLightNum+"");
        tvAboutus.setText(getVersionName(mContext));
    }

    @OnClick(R.id.iv_editPadId)
    public void onIvEditPadIdClicked() {
        final EditText et = new EditText(mContext);
        et.setInputType(InputType.TYPE_CLASS_NUMBER);
        new AlertDialog.Builder(mContext).setTitle("请输入平板ID")
                .setView(et)
                .setIcon(R.drawable.ic_edit)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //按下确定键后的事件
                        String padId = et.getText().toString().trim();
                        if (!oldPadId.equalsIgnoreCase(padId)){
                            Constants.PADID = Short.parseShort(padId);
                            tvPadId.setText(padId);
                            Setting setting = new Setting();
                            setting.setSettingName(Setting.NAME_PADID);
                            setting.setSettingValue(padId+"");
                            settingDao.addSetting(setting);
                            //发送指令给adapter
                            PadIDCMD padIDCMD = new PadIDCMD();
                            padIDCMD.setPadId(Short.parseShort(padId));
                            BaseSerialActivity activity = (BaseSerialActivity)mContext;
                            activity.writeSerialByte(padIDCMD.getBytes());
                        }
                    }
                }).setNegativeButton("取消",null).show();
    }

    @OnClick(R.id.iv_editLightNum)
    public void onIvEditLightNumClicked() {
        final EditText et = new EditText(mContext);
        et.setInputType(InputType.TYPE_CLASS_NUMBER);
        new AlertDialog.Builder(mContext).setTitle("请输入繁星灯数量")
                .setView(et)
                .setIcon(R.drawable.ic_edit)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String lightNumStr = et.getText().toString().trim();
                        int lightNum = Integer.parseInt(lightNumStr);
                        if (oldLightNum!=lightNum){
                            tvLightNum.setText(lightNumStr);
                            if (oldLightNum<lightNum){
                                //新增light
                                for (int m =oldLightNum+1;m<=lightNum;m++){
                                    Light light = new Light(m);
                                    lightDao.addLight(light);
                                }
                            } else {
                                //删除light
                                for (int j=oldLightNum;j>lightNum;j--){
                                    try {
                                        lightDao.deleteLight(j);
                                    } catch (SQLException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                }).setNegativeButton("取消",null).show();
    }



    public String getVersionName(Context context) {
        String versionName=null;
        try {
            versionName=context.getPackageManager().getPackageInfo(context.getPackageName(),0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return String.format(context.getResources().getString(R.string.splash_version),versionName);
    }
}
